package zd.cornermemory.view;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import zd.cornermemory.R;

import static zd.cornermemory.utils.Configs.scale;

public class TimerDialog extends Dialog {
	public TimerDialog(Context context) {
		super(context);
	}

	public TimerDialog(Context context, int theme) {
		super(context, theme);
	}
	
	public void show() {
		Window dw = getWindow();
		WindowManager.LayoutParams p = dw.getAttributes();
		p.width = (int) Math.round(scale * 260);;
		dw.setAttributes(p);
		setCanceledOnTouchOutside(false);
		super.show();
	}

	public static class Builder {
		private OnClickListener mOnClickListener;
		private Context context;
		private String timerStr;

		public void setTimerStr(String timerStr) {
			this.timerStr = timerStr;
		}

		public Builder(Context context) {
			this.context = context;
		}

		public void setmOnClickListener(OnClickListener mOnClickListener) {
			this.mOnClickListener = mOnClickListener;
		}

		public TimerDialog create() {
			LayoutInflater inflater = LayoutInflater.from(context);
			final TimerDialog dialog = new TimerDialog(context, R.style.TimerDialog);
			int resId = R.layout.timer_dialog;
			View layout = inflater.inflate(resId, null);
			dialog.addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			TextView no = layout.findViewById(R.id.no);
			TextView add2 = layout.findViewById(R.id.add2);
			TextView dnf = layout.findViewById(R.id.dnf);
			TextView cancel = layout.findViewById(R.id.cancel);
			TextView title = layout.findViewById(R.id.title);
			title.setText(timerStr);
			if (mOnClickListener != null) {
				no.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						mOnClickListener.onClick(dialog,0);
						dialog.dismiss();
					}
				});
				add2.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						mOnClickListener.onClick(dialog,1);
						dialog.dismiss();
					}
				});
				dnf.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						mOnClickListener.onClick(dialog,2);
						dialog.dismiss();
					}
				});
				cancel.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						mOnClickListener.onClick(dialog,3);
						dialog.dismiss();
					}
				});
				dialog.setOnCancelListener(new OnCancelListener() {
					@Override
					public void onCancel(DialogInterface dialogInterface) {
						mOnClickListener.onClick(dialog,3);
						dialog.dismiss();
					}
				});
			}
			return dialog;
		}
		
		public void show() {
			create().show();
		}

	}
}
